<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="auto"
      v-show="showSearch"
    >
      <el-row>
        <el-form-item label="下单日期" style="width: 350px">
          <el-date-picker
            v-model="dateRange"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="生产单编号" prop="produceNo">
          <el-input
            v-model.trim="queryParams.produceNo"
            style="width: 180px"
            placeholder="请输入生产单编号"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="出版编号" prop="pubNo">
          <el-input
            v-model.trim="queryParams.pubNo"
            style="width: 180px"
            placeholder="请输入出版编号"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="机台名称" prop="machineName">
          <el-input
            v-model.trim="queryParams.machineName"
            style="width: 180px"
            placeholder="请输入机台名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="车间" prop="workshopName">
          <el-select
            style="width: 100%"
            v-model="queryParams.workshopName"
            placeholder="请选择"
          >
            <el-option
              v-for="item in ems_workshop"
              :key="item.value"
              :label="item.label"
              :value="item.label"
            />
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="产品名称" prop="shipmentType">
          <el-input
            v-model.trim="queryParams.shipmentType"
            style="width: 180px"
            placeholder="请输入产品名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
            <el-button type="primary" plain icon="Download" @click="handleExport">导出</el-button>
        </el-col> -->
    </el-row>
    <div>
      <el-table
        v-loading="loading"
        border
        height="65vh"
        :data="dataList"
        @expand-change="handleHeaderClick"
        :cell-style="tableCellStyle"
      >
        <el-table-column label="序号" width="60" type="index" align="center" />
        <el-table-column
          align="center"
          label="下单日期"
          min-width="120"
          prop="submitDate"
        >
          <template v-slot="{ row }">
            {{ row.submitDate && row.submitDate.split("T")[0] }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          min-width="170"
          label="生产单编号"
          prop="produceNo"
        />
        <el-table-column
          align="center"
          min-width="170"
          label="出版编号"
          prop="pubNo"
        />
        <el-table-column
          min-width="280"
          align="center"
          label="产品名称"
          prop="shipmentType"
        />

        <el-table-column align="center" label="品种" prop="breed" />
        <el-table-column align="center" label="贴数" prop="pieceView" />
        <el-table-column align="center" label="制版">
          <el-table-column align="center" label="状态" prop="isTake">
            <template v-slot="{ row }">
              {{ row.isTake == 2 ? "制版完" : "未制版" }}
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column align="center" label="印刷">
          <el-table-column align="center" label="状态" prop="pressState">
            <template v-slot="{ row }">
              <span>
                {{ row.pressState == 2 ? "印刷完" : "已排产" }}
              </span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="机台" prop="machineName" />
          <el-table-column align="center" label="机长" prop="userName" />
          <el-table-column align="center" label="计划量" prop="printAmount" />
          <el-table-column align="center" label="完工量" prop="finishNum" />
        </el-table-column>

        <el-table-column align="center" label="折页">
          <el-table-column align="center" label="状态" prop="pressState">
            <template v-slot="{ row }">
              {{
                row.foldState == 2
                  ? "折页完"
                  : row.ZYMachineName == null
                  ? "--"
                  : "已排产"
              }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="机台" prop="ZYMachineName" />
          <el-table-column align="center" label="机长" prop="ZYUserName" />
          <el-table-column align="center" label="计划量" prop="ZYPrintAmount" />
          <el-table-column align="center" label="完工量" prop="ZYFinishNum" />
        </el-table-column>
        <el-table-column align="center" label="订单数量" prop="orderCount" />
        <el-table-column align="center" label="出版编号" prop="pubNo" />
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script setup>
import { selectNWProducePrint } from "@/api/production/textPrinting.js";

const { proxy } = getCurrentInstance();
const loading = ref(true);
const dataList = ref([]);
const showSearch = ref(true);
const router = useRouter();
const total = ref(0);
const advanced = ref(false);
const sheetTotal = ref(0);
const platemakingTotal = ref(0);
// 列显隐信息
const columns = ref([
  { key: 0, label: `状态`, visible: true },
  { key: 1, label: `新/旧版`, visible: true },
  { key: 2, label: `制版日期`, visible: true },
  { key: 3, label: `制版编码`, visible: true },
  { key: 4, label: `生产单编码`, visible: true },
  { key: 5, label: `出版编码`, visible: true },
  { key: 6, label: `书刊编码`, visible: true },
  { key: 7, label: `书刊名称`, visible: true },
  { key: 8, label: `印刷序列号`, visible: true },
  { key: 9, label: `品种`, visible: true },
  { key: 10, label: `组成`, visible: true },
  { key: 11, label: `印张`, visible: true },
  { key: 12, label: `印数`, visible: true },
  { key: 13, label: `印色`, visible: true },
  { key: 14, label: `版材数量`, visible: true },
  { key: 15, label: `制版数量`, visible: true },
  { key: 16, label: `版材尺寸`, visible: true },
  { key: 17, label: `制版人`, visible: true },
  { key: 18, label: `烤版人`, visible: true },
  { key: 19, label: `完成日期`, visible: true },
  { key: 20, label: `取版人`, visible: true },
  { key: 21, label: `取版日期`, visible: true },
  { key: 22, label: `备注`, visible: true },
]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
});

const getDateNow = () => {
  var myDate = new Date();
  var year = myDate.getFullYear();
  var month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月，所以要加1);
  var day = myDate.getDate(); //获取当前日（1-31）
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  var firstDay = year + "-" + month + "-01";
  return firstDay;
};
const getCurrentDay = () => {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth() + 1; // 月份从0开始，需要加1
  var day = now.getDate();
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  const currentDay = year + "-" + month + "-" + day;
  return currentDay;
};
const dateRange = ref([getDateNow(), getCurrentDay()]);
const { queryParams } = toRefs(data);
const {
  ems_workshop,
  ems_machine_info_size,
  ems_order_binding_mode,
  ems_machine_info_type,
  ems_machine_info_model,
  ems_order_status,
} = proxy.useDict(
  "ems_workshop",
  "ems_machine_info_type",
  "ems_order_status",
  "ems_machine_info_model",
  "ems_machine_info_size",
  "ems_order_binding_mode"
);

const getList = () => {
  loading.value = true;
  queryParams.value.startDate = Array.isArray(dateRange.value)
    ? dateRange.value[0]
    : undefined;
  queryParams.value.endDate =
    Array.isArray(dateRange.value) > 0 ? dateRange.value[1] : undefined;
  selectNWProducePrint(queryParams.value)
    .then((res) => {
      dataList.value = res.rows;
      loading.value = false;
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};
function tableCellStyle({ row, column, rowIndex, columnIndex }) {
  if (columnIndex === 8) {
    if (row.pressState == "2") {
      return { "background-color": "#58BC52", color: "#fff" };
    }
  }
  if (columnIndex === 13) {
    if (row.foldState == "2") {
      return { "background-color": "#58BC52", color: "#fff" };
    }
  }
}

// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = [getDateNow(), getCurrentDay()];
  proxy.resetForm("queryRef");
  handleQuery();
};

const handleViewCode = (row) => {
  router.push({
    path: "/production/platemaking/platemakingDetails",
    query: {
      id: row.platemakingId,
      info: true,
    },
  });
};

getList();
// router.beforeEach((to, from, next) => {
//     if (to.path == "/basicInfo/personnel") {
//         getList()
//     }
//     next();
// });
</script>

<style lang="scss" scoped>
.operation {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-bottom: 18px;
}
</style>
